package com.lyw.i14CompleteFuture;

import com.sun.xml.internal.ws.util.CompletedFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * 异步调用
 *    - 有返回值的
 *    - 没有返回值的
 *
 * @author lyw
 * @Date 2023/7/11-下午11:25
 */
public class CompleteFutureDemo {

  public static void main(String[] args) throws Exception {
    CompletableFuture<Void> completedFuture = CompletableFuture.runAsync(() -> {
      System.out.println(Thread.currentThread().getName() + " -》 异步调用无返回值");
    });
    completedFuture.get();

    CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
      System.out.println(Thread.currentThread().getName() + " -》 异步调用有返回值的");
      return 1;
    });
    completableFuture.whenComplete((result, exception) -> {
      System.out.println("----Result = " + result);
      System.out.println("----Exception = " + exception);
    }).get();
  }
}
